package com.sddr.config.apollo.operation;

import com.sddr.base.frame.metadata.MetaDataOperation;
import com.sddr.common.aoollo.api.ApolloApi;
import com.sddr.common.aoollo.api.ApolloListen;
import com.sddr.config.observe.ConfigNotify;
import com.sddr.config.operation.AbstractRemoteConfigRuleOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 山顶洞人郑某
 * @className ApolloRemoteConfigRuleOperation.java 阿波罗配置中心操作
 * @date 2025 07 13
 */
@Slf4j
public class ApolloRemoteConfigRuleOperation extends AbstractRemoteConfigRuleOperation {


    private final ApolloApi apolloApi;
    public ApolloRemoteConfigRuleOperation(ApolloApi apolloApi, final MetaDataOperation metaDataOperation, ConfigNotify configNotify) {
        super(metaDataOperation,configNotify);
        this.apolloApi = apolloApi;
    }

    /**
     * 获取配置信息
     *
     * @param group  组
     * @param dataId 数据Id
     * @return 配置信息
     */
    @Override
    public String getDefiniteConfig(String group, String dataId) throws Exception {
        return apolloApi.getConfig(dataId, group);
    }

    /**
     * 监听配置信息
     */
    @Override
    public void linstenConfig() {
        String group = getGroup();
        String dataId = getDataId(false);
        String generaldataId = getDataId(true);
        try {
            apolloApi.listenConfig(dataId,group,(this::callbackConfig));
            apolloApi.listenConfig(generaldataId,group,(this::callbackConfig));
        } catch (Exception e) {
            log.error("监听配置信息失败", e);
        }
    }


}
